package com.jindu.producer.system.jpa;

import com.jindu.producer.system.model.SUsersEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author jarvis
 * @date 2018-2-20
 */
@Transactional
public interface UsersJpa extends JpaRepository<SUsersEntity, String>,JpaSpecificationExecutor {

    SUsersEntity findById(String id);


    /**
     * 根据账户或mobile查询登录信息，非后台账户登录，状态为激活。
     * @param account
     * @return
     */
    @Query("select u from SUsersEntity u where ((u.account = ?1) or (u.mobile= ?1) or (u.email= ?1))")
    SUsersEntity findByAccountOrMobileOrEmail(String account);


    @Query("select u from SUsersEntity u where u.mobile = ?1")
    SUsersEntity findByMobile(String mobile);

    @Query("select u from SUsersEntity u where u.email = ?1")
    SUsersEntity findByEmail(String email);


    /**
     * 管理后台登录 ,必须是账户登录，状态为激活，登录类型为后台登录
     * @param account
     * @return
     */
    @Query("select u from SUsersEntity u where u.account = ?1 and u.loginType= 1 and u.status= 1")
    SUsersEntity findByAccount(String account);

    /**
     * 根据openId查询该用户是否存在。
     * @param openId
     * @return
     */
    SUsersEntity findByOpenId(String openId);

    /**
     * 根据用户的ids，查询所有对象
     * @param id
     * @return
     */
    List<SUsersEntity> findByIdIn(List<String> id);

    /**
     * 根据用户输入的名称，查询用户的信息，返回Id
     * @param name
     * @return
     */
    @Query("select u from SUsersEntity u where ((u.account like CONCAT('%',?1,'%')) or (u.chineseName like CONCAT('%',?1,'%')) or (u.nickname like CONCAT('%',?1,'%'))) ")
    List<SUsersEntity> findByUserName(String name);

}
